<?php

namespace Admin\Controller;

use Think\Upload;

class UploadController extends CommonController
{
    /**
     * [attach 附件上传]
     */
    public function img()
    {
        $type = I('param.type', '', 'trim');

        if (IS_POST) {
            if (!empty($_FILES[$type]['name'])) {
                if (empty($img)) {
                    $maxSize = 2097152;
                    $typecase = array(
                        'question_img' => C('YTATA_IMG_QUESTION_DIR'),
                    );
                    $rootpath = $typecase[$type];
                    $upload = new Upload(array(), 'local');// 实例化上传类(使用本地上传驱动)
                    $upload->maxSize = $maxSize;// 设置附件上传大小
                    $upload->autoSub = false;
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath = $rootpath; // 设置附件上传根目录
                    $upload->savePath = date('Y/m/'); // 设置附件上传（子）目录
                    $upload->saveName = array('uniqid', '');

                    // 上传文件
                    $info = $upload->uploadOne($_FILES[$type]);
                    if (!$info) {// 上传错误提示错误信息
                        $ajax['status'] = 0;
                        $ajax['content'] = $upload->getError();
                        $this->ajaxReturn($ajax);
                        exit;
                    } else {
                        // 上传成功
                        $info['src'] = $rootpath . $info['savepath'] . $info['savename'];
                        $ajax['status'] = 1;
                        $ajax['content'] = __ROOT__ . '/' . $info['src'];
                        $ajax['save'] = $info['src'];
                        $this->ajaxReturn($ajax);
                    }
                }
            } else {
                $ajax['status'] = 0;
                $ajax['content'] = "参数非法";
                $this->ajaxReturn($ajax);
            }
        }
    }
}
?>